package com.grad.why.studentsystem.controller;

import com.grad.why.studentsystem.domain.ClassRoom;
import com.grad.why.studentsystem.domain.current.CurrentClassinfo;
import com.grad.why.studentsystem.service.ClassRoomService;
import com.grad.why.studentsystem.service.OptionparamsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.swing.text.html.Option;
import java.io.File;
import java.io.FileOutputStream;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/studentManageSystem")
public class ClassRoomController {
    @Autowired
    private ClassRoomService classRoomService;

    @Autowired
    private OptionparamsService optionparamsService;

    @RequestMapping("/getClassRoomInfos")
    @ResponseBody
    public Object getClassInfoList(
            @RequestParam(required = false, defaultValue = "0", value = "offset") int offset,
            @RequestParam(required = false, defaultValue = "10", value = "limit") int limit,
            @RequestParam(required = false, defaultValue = "", value = "search") String search
    ) {
        try {
            List<ClassRoom> rows = classRoomService.getClassRoomInfoList(offset,limit,search);
            long total = classRoomService.getClassRoomInfoListLength(search);
            Map<String, Object> result = new HashMap<>(2);
            result.put("rows", rows);
            result.put("total", total);
            return result;
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/deleteClassRoomInfosByIds/{ids}")
    @ResponseBody
    public String deleteClassRoomInfosByIds(@PathVariable("ids") String ids) {
        try{
            String[] idArray = ids.split("-");
            for(String s :idArray){
                int rows = classRoomService.deleteClassRoomInfosById(Integer.parseInt(s));
                if(rows <= 0){
                    return null;
                }
                return "OK";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/addClassRoomInfo")
    @ResponseBody
    public String addClassRoomInfo(ClassRoom classRoom){
        try{
            String classRoomName = classRoom.getClassroomname();
            int isExist = classRoomService.isClassRoomExist(classRoomName);
            if(isExist <= 0){
                int maxRid = classRoomService.getMaxrid() + 1;
                classRoom.setRid(maxRid);
                int rows = classRoomService.addClassRoomInfo(classRoom);
                if(rows > 0){
                    return "OK";
                }else{
                    return null;
                }
            }else{
                return "该教室已经存在,请重试!";
            }
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }

    @RequestMapping("/uploadClassRoomInfos")
    @ResponseBody
    public String uploadClassRoomInfos(@RequestParam("file")MultipartFile file){
        try{
            String fileUploadRootPath = optionparamsService.getParamValueByParamName("fileRootPath");
            String fileName = file.getOriginalFilename();
            String uploadCurrentFileName = fileUploadRootPath+"uploadClassRoomInfo/"+ new Date().getTime() + fileName;

            File fileRootPath = new File(fileUploadRootPath+"uploadClassRoomInfo");

            if(!fileRootPath.exists()){
                fileRootPath.mkdirs();
            }

            FileOutputStream out = new FileOutputStream(uploadCurrentFileName);
            out.write(file.getBytes());
            out.flush();
            out.close();

            int n = classRoomService.addClassRoomInfosByExcelFile(uploadCurrentFileName);
            if(n<=0){
                return "添加失败";
            }else{
                return "添加"+n+"条数据";
            }
            //上传学生信息逻辑
        }catch (Exception e){
            e.printStackTrace();
        }
        return null;
    }
}
